package Email.auto.checker;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;

public class ClickButtonOk implements ActionListener {

	private Tray tray;
	private Account account;
	private String user;
	private String domain;
	private String[] part = { "part1", "part2" };
	private Comunication comunication;

	private Timer t;

	public ClickButtonOk(Account acc, Comunication com, Tray trayMenu) {
		tray = trayMenu;
		account = acc;
		comunication = com;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		String email = account.getTextEmail().getText();
		String passw = new String(account.getTextPass().getPassword());

		boolean result = verifyFields(email, passw);
		if (result == false) {
			account.getTextEmail()
					.setBorder(
							BorderFactory.createTitledBorder(
									BorderFactory.createLineBorder(Color.RED),
									"Email"));
		} else {
			account.getTextEmail()
					.setBorder(
							BorderFactory.createTitledBorder(
									BorderFactory.createLineBorder(Color.GRAY),
									"Email"));

			account.setVisible(false);
			account.setDomain(domain);
			account.getTray().getMenuItemEmailInformation().setText(email);
			account.getTray().getMenuItemInsertEmail().setEnabled(false);
			account.getTray().getMenuItemRemoveEmail().setEnabled(true);
			account.getTray().getMenuItemTimeout().setEnabled(true);
			account.getTray().getMenuItemVerifyMessg().setEnabled(true);
			account.getTray().getMenuItemTimeout().setEnabled(true);

			t = new Timer();
			TimerTask timerControl = new TimerControl(comunication, account);
			t.scheduleAtFixedRate(timerControl,
					ConstComunication.FIFTEEN_MINUTES,
					ConstComunication.FIFTEEN_MINUTES);

			tray.setT(t);

			account.setEmail(email);
			account.setPass(passw);
		}
	}

	public boolean verifyFields(String mail, String pass) {
		if (mail.contains("@")) {
			part = mail.split("@");
			user = part[0];
			domain = part[1];

			if (!(domain.equals(ConstAccount.ISEC_DOMAIN))
					&& !(domain.equals(ConstAccount.GMAIL_DOMAIN))
					&& !(domain.equals(ConstAccount.HOTMAIL_DOMAIN))) {
				System.out.println("Dominio Errado");
				return false;
			}
			if (domain.equals(ConstAccount.ISEC_DOMAIN)) {
				if (comunication.setServer(ConstComunication.ISEC_SERVER) == ConstComunication.COMUNICATION_OK) {
					String login_command_isec = ConstComunication.LOGIN_IMAP
							+ " " + user + " " + pass;
					if (comunication.login(login_command_isec, null) == true) {
						System.out.println("Login: Ok");
						return true;
					}
					System.out.println("Login Error");
				}
			} else if (domain.equals(ConstAccount.GMAIL_DOMAIN)) {
				if (comunication.setServer(ConstComunication.GMAIL_SERVER) == ConstComunication.COMUNICATION_OK) {
					String login_command_gmail = ConstComunication.LOGIN_IMAP
							+ " " + user + " " + pass;
					if (comunication.login(login_command_gmail, null) == true) {
						System.out.println("Login: Ok");
						return true;
					}
					System.out.println("Login Error");
				}

			}

		}
		return false;
	}
}
